$(function () {
    var baseUrl = window.location.protocol + "//" + window.location.host;

    layui.use(['layer','layedit','jquery'], function () {

        //添加查看记录
        var viewRecordCount=$('input[name=viewRecordCount]').val();
        if(viewRecordCount<1){
            if($('input[name=article_id]').val()==''){ layer.msg('文章不存在！', {icon: 5,time: 2000}); return false; }
            var data={
                article_id: $('input[name=article_id]').val(),
            };

            $.ajax({
                type: "POST",
                url: baseUrl+"/api/article/addViewRecord",
                data: data,
                dataType: "json",
                success: function(data){
                    if(data.code==1){
                    }else{
                        layer.msg(data.msg, {icon: 5,time: 2000});
                        return false;
                    }
                }
            });
        }

        //文章评论
        $('#articleCommentS').on('click',function () {
            if($(this).attr('data-status')==1) return false;

            if($(this).parent().find('textarea').val()==''){ layer.msg('请输入文章评论！', {icon: 5,time: 2000}); return false; }

            if($('input[name=article_id]').val()==''){ layer.msg('文章不存在！', {icon: 5,time: 2000}); return false; }

            if($('input[name=username]').val()==''){ layer.msg('请输入用户名！', {icon: 5,time: 2000}); return false; }

            if($('input[name=captcha]').val()==''){ layer.msg('请输入验证码！', {icon: 5,time: 2000}); return false; }

            $(this).attr('data-status',1);

            var that=$(this);

            var data={
                content:$(this).parent().find('textarea').val(),
                article_id: $('input[name=article_id]').val(),
                username: $('input[name=username]').val(),
                captcha: $('input[name=captcha]').val(),
            };

            $.ajax({
                type: "POST",
                url: baseUrl+"/api/article/comment",
                data: data,
                dataType: "json",
                success: function(data){
                    if(data.code==1){
                        layer.msg(data.msg, {
                            icon: 6,
                            time: 1000
                        }, function(){
                            that.attr('data-status',0);
                            window.location.reload();
                        });
                    }else{
                        that.attr('data-status',0);
                        layer.msg(data.msg, {icon: 5,time: 2000});
                        return false;
                    }
                }
            });

        });

        //文章点赞
        $('.diggit').on('click',function () {
            if($(this).attr('data-status')==1) return false;

            if($('input[name=article_id]').val()==''){ layer.msg('文章不存在！', {icon: 5,time: 2000}); return false; }

            $(this).attr('data-status',1);

            var that=$(this);

            var data={
                article_id: $('input[name=article_id]').val(),
            };

            $.ajax({
                type: "POST",
                url: baseUrl+"/api/article/addZanRecord",
                data: data,
                dataType: "json",
                success: function(data){
                    if(data.code==1){
                        layer.msg(data.msg, {
                            icon: 6,
                            time: 1000
                        }, function(){
                            that.attr('data-status',0);
                            $('#diggnum').text(data.data.zan);
                        });
                    }else{
                        that.attr('data-status',0);
                        layer.msg(data.msg, {icon: 5,time: 2000});
                        return false;
                    }
                }
            });

        });

        // 点击打赏
        $('.reward').on('click',function () {
           if($(this).attr('status')==1){
               $(this).find('img').hide();
               $(this).removeAttr('status');
           }else{
               $(this).find('img').show();
               $(this).attr('status',1);
           }
        });

    });
});